package com.woniuxy.cloud.carl.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.user.client.UserQueryClient;
import com.woniuxy.cloud.carl.user.client.param.GetByConditionParam;
import com.woniuxy.cloud.carl.user.client.result.UserDetailDto;
import com.woniuxy.cloud.carl.user.client.result.UserForStationDto;
import com.woniuxy.cloud.carl.user.client.result.WoniuPageDto;
import com.woniuxy.cloud.carl.user.common.ContantsDate;
import com.woniuxy.cloud.carl.user.common.UserResultCode;
import com.woniuxy.cloud.carl.user.infra.model.User;
import com.woniuxy.cloud.carl.user.service.UserService;
import com.woniuxy.cloud.carl.user.service.param.GetUserByConditionServiceParam;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserQueryApiImpl implements UserQueryClient {

    @Resource
    private UserService userService;
    @Override
    @Log(value = "获得某用户详情")
    public Result<UserDetailDto> getUserDetail(Long userId) {
        User user = userService.getById(userId);
        if(user==null) throw new MallException(UserResultCode.USER_NOT_EXIST);
        UserDetailDto userDetailDto = BeanUtil.toBean(user, UserDetailDto.class);
        return Result.success(userDetailDto);
    }

    @Override
    @Log(value = "按条件查询用户")
    public Result<WoniuPageDto<UserForStationDto>> getByCondition(GetByConditionParam param) {
        GetUserByConditionServiceParam serviceParam = BeanUtil.toBean(param, GetUserByConditionServiceParam.class);
        Page<User> page = userService.getByCondition(serviceParam);
        List<UserForStationDto> dtos = new ArrayList<>();
        for (User u : page.getRecords()) {
            UserForStationDto dto = BeanUtil.toBean(u, UserForStationDto.class);
            dto.setVipState(dto.getVip()==ContantsDate.USER_VIP?"会员":"非会员");
            dto.setTalkState(dto.getUserState()==ContantsDate.USER_STATE_NORMAL?"正常":"已禁言");
            dtos.add(dto);
        }
        WoniuPageDto<UserForStationDto> woniuPageDto = BeanUtil.toBean(page, WoniuPageDto.class);
        woniuPageDto.setList(dtos);
        return Result.success(woniuPageDto);
    }
}
